/**
 * 所有动作的基类
 *
 */
module tool {
    export class Animation extends eui.Image {
        /** 序列帧图集的配置文件 */
        private _souceJSON: string = "";
        public set souceJSON(obj: Object) {
            this._souceJSON = obj["json"];
            this.isReplay = obj["isReplay"];
            this.isAutoPlay = obj["isAutoPlay"];
            this.xSpeed = obj["xSpeed"];
            this.ySpeed = obj["ySpeed"];
            this.isJumping = obj["isJumping"];
            this.isOnce = obj["isOnce"];
            this.frameRate = obj["frameRate"] || 14;
            this.lastAct = obj["lastAct"] || 1;
            this.getResouce();
        }
        public get souceJSON() {
            return this._souceJSON;
        }
        public sWidth = 0;
        public sHeight = 0;
        /** 是否循环 */
        public isReplay: boolean = true;
        /** 是否自动播放*/
        public isAutoPlay: boolean = true;
        /** 是否是跳跃 **/
        public isJumping: boolean = false;
        /** 是否播放完整**/
        public isOnce: boolean = false;
        /** 帧率 */
        public frameRate: number = 14;
        /** 横向速度*/
        public xSpeed: number = 0;
        /** 纵向速度*/
        public ySpeed: number = 0;
        /** 后续动作*/
        public lastAct: number = 1;
        /**
         * 是否正在播放（只读）
         */
        public get isPlaying() {
            return this._isPlaying;
        }
        // 是否正在播放
        private _isPlaying: boolean = false;
        // 图集组
        private _sheet: Array<string>;
        // 图集角标
        private _sheetMark: number = -1;
        // 时间ID 
        private timeID: number = 0;
        //完成事件
        public compeleEvent: egret.Event;
        /** 图片路径 */
        public static souceRul: string = "resource/packs";
        public constructor() {
            super();
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.start,this);
        }
        public start() {
            this.sWidth = this.stage.stageWidth;
            this.sHeight = this.stage.stageHeight;
        }
        //加载解析JOSN文件
        public getResouce() {
            var url = Animation.souceRul + "/" + this.souceJSON;
            RES.getResByUrl(url,this.setSouceJSON,this);
        }
        //整理解析的数据
        private setSouceJSON(data: any,url: string) {
            this._sheet = [];
            for(var key in data.frames) {
                this._sheet.push(key);
            }
            this._sheet.sort((a: string,b: string): number => {
                var anum: number = parseInt(a.replace(/[^1-9]/ig,""));
                var bnum: number = parseInt(a.replace(/[^0-9]/ig,""));
                if(anum < bnum) {
                    return -1;
                }
                else if(anum > bnum) {
                    return 1;
                }
                else {
                    return 0;
                }
            });
            this.refersh();
        }
        /**
        * 帧动画当前所在帧帧
        */
        public get sheetMark() {
            return this._sheetMark;
        }
        
        /** 刷新 */
        public refersh() {
            this.stop();
            this.play();
        }
        
        /** 停止*/
        public stop() {
            this._sheetMark = 0;
            egret.clearInterval(this.timeID);
            this._isPlaying = false;
        }
        
        /** 开始 */
        public play() {
            if(this._isPlaying) {
                this.stop();
            }
            this.timeID = egret.setInterval(this.onInterval,this,1000 / this.frameRate);
            this._isPlaying = true;
        }

        private onInterval() {
            this.source = this._sheet[this.sheetMark];
            //限制界限
            var bool = (this.left - this.xSpeed >= -74 && this.sWidth - this.x - this.xSpeed >= -74 && this.bottom >= 20 && this.y >= 40)
            if(!this.isJumping) {//是否是跳跃
                this.moveGoto(bool);
            }
            else {
                this.jumping(bool);
            }
            this._sheetMark++;
            if(this._sheetMark == this._sheet.length) {
                if(!this.isAutoPlay) {//执行完毕保持最后一个姿势
                    egret.clearInterval(this.timeID);
                    this.source = this._sheet[this._sheetMark - 1];
                }
                else {
                    this.onComplete();
                }
            }
        }
        /**
         * 移动
         */
        public moveGoto(bool: boolean) {
            if(bool) {
                this.left += this.xSpeed;
                this.bottom += this.ySpeed;
            }
        }
        /**
         * 跳跃
         */
        public jumping(bool: boolean) {
            var len = this._sheet.length;
            if(this.sheetMark < len / 2) {
                this.bottom += this.ySpeed;
            }
            else {
                this.bottom -= this.ySpeed;
            }
            this.left += this.xSpeed;
        }
        public onComplete() { };
    }
}  